/* eslint-disable */

import styles from './BasicLayout.less';

import React, { Component } from 'react'
import { ConfigProvider, Layout } from 'antd';
import { connect } from 'dva';

import GlobalHeader from '@/components/GlobalHeader'
import { history } from "umi"
// import enUS from 'antd/es/locale/en_US';
import zhCN from 'antd/es/locale/zh_CN';
import HebPlayerPort from '@/components/VideoPlayer/hebPlayerLoader'
// import { notification } from 'antd';

import router from 'umi/router';
import { arrayUnique } from '@/utils/utils';

import { getOnlineDevice } from '@/services/device'

const { Content } = Layout;

var sosList = []

@connect(({ config, device, sos, loading }) => ({
  config,
  device,
  sos,
  loading: loading.models.config,
}))
class BasicLayout extends Component {
  state = {
    collapsed: false,
    siderWidth: 200,
    maxSiderWidth: 200,
    minSiderWidth: 80,
  };

  componentDidMount() {

    this.props.dispatch({
      type: 'device/fetchDeviceList',
      callback: ({ data }) => {
        this.openSOS(data)
      }
    })
  }

  openSOS = async () => {

    const onlineDevice = await getOnlineDevice()
    const deviceList = onlineDevice.data

    let sosListStr = this.props.location.query.sosList

    if (sosListStr != '' && sosListStr != undefined) {
      sosList = sosListStr.split(',')
      window.sosList = sosList
      router.push({
        pathname: '/',
      });
    }

    this.props.dispatch({
      type: 'config/fetchInfo',
      callback: res => {
        let data = res.data
        let jsApiUrl = data.videoJsapiUrl
        // 挂在window下
        window.videoJsapiUrl = jsApiUrl
        // let jsApiUrl = 'http://172.21.6.106:11004'
        let ret = HebPlayerPort.init({
          jsPath: jsApiUrl + '/player/hebVideo.safe.js',
          callback: (token, taskID, containerID, result, detail) => {
            if (window._zbkHebMode) {
              window._zbkHebMode(2)
            }
            if (result === 'forcePlay') {
              if (detail != undefined) {

                let device = deviceList.filter(item => {
                  return item.gbId == detail
                })
                // 临时解决方案
                if (device.length) {
                  console.log('存在列表')
                  sosList.push(detail)
                  sosList = arrayUnique(sosList)
                  window.sosList = sosList
                } else {
                  console.log('不存在列表')
                }
              }
              router.push({
                pathname: '/',
              });
            }
            if ('plugin' === result) {
              if ('OK' === detail) {
                window.loadPlugin = true
              } else {
                window.loadPlugin = false
              }
            }
          }
        });
        if (0 !== ret) {
          window.LoadVideoError = true;
        }
      }
    })
  }

  playVideo() {

  }

  getDeviceInfo(id) {

    // this.props.dispatch({
    //   type: 'device/fetchDeviceInfo',
    //   payload: {
    //     sn: 'ESWSWS4'
    //   }
    // })
  }

  // 以后转移到 DidMount
  UNSAFE_componentWillMount() {
    let auth = this.props.location.query.auth
    let _sosList = this.props.location.query.sosList

    let url = this.props.location.pathname;
    if (auth != '' && auth != undefined) {
      window.sessionStorage.setItem('auth', auth);
      if (_sosList != '' && _sosList != undefined) {
        url = url + '?sosList=' + _sosList
      }
      window.location.href = url
    }
  }

  render() {
    return (
      <ConfigProvider locale={zhCN}>
        <Layout className={styles.layout}>
          <GlobalHeader />
          <Content className={styles.container}>
            {this.props.children}
          </Content>
        </Layout>
      </ConfigProvider>
    )
  }
}
export default BasicLayout